<?php
namespace app\jobmanagement\service;


use think\Exception;
use app\model\Menu as mMenu;
use app\model\TaskTask as mTask;
use app\model\Settle as mSettle;

class Index
{
    const SGLY = 9; // 超级管理员
    const GLY = 1 ; //管理员权限
    const XZZ = 2; //小组长
    const ZY = 3; //组员
    const JTSHY = 4; //阶梯审核员

    private $sCommon;
    private $mMenu;
    private $mTask;
    private $mSettle;

    public function __construct() {
        $this->mMenu = new mMenu();
        $this->sCommon = new Common();
        $this->mTask = new mTask();
        $this->mSettle = new mSettle();
    }

    /**
     * @introduction:获取小程序首页所有菜单列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/5/19 18:52
     */
    public function menuList() {
        $is_sys = session('is_sys');
        $dd_user_id = session('dd_user_id');
        $dept_id = session('dept_id');
        //此处为了给技术部测试用设置
        if($dd_user_id == '15881473841741654' or $dd_user_id == '0934184711639064'){ //15882337920309437蔡先华，15881473841741654原俊明，0934184711639064丁丹
            $is_sys = 0;
        }
        switch ($is_sys){
            case 1 :
                $menuList = $this->mMenu
                    ->field('id,p_id,name,level,icon,url, 0 as num')
                    ->where('','exp','FIND_IN_SET('.self::SGLY.',auth)')
                    ->select();
                break;
            case 0 :
                $userInfo = $this->sCommon->getUserInfo($dd_user_id);
                if(!empty($userInfo) and $userInfo['level'] == 4){
                    $menuList = $this->mMenu
                        ->field('id,p_id,name,level,icon,url, 0 as num')
                        ->where('','exp','FIND_IN_SET('.self::JTSHY.',auth)')
                        ->select();
                }else if(!empty($userInfo) and $userInfo['level'] == 3){
                $menuList = $this->mMenu
                    ->field('id,p_id,name,level,icon,url, 0 as num')
                    ->where('','exp','FIND_IN_SET('.self::ZY.',auth)')
                    ->select();
                }else if ($userInfo['level'] == 2){
                    $menuList = $this->mMenu
                        ->field('id,p_id,name,level,icon,url, 0 as num')
                        ->where('','exp','FIND_IN_SET('.self::XZZ.',auth)')
                        ->select();
                    // 待审核任务单2需要查询数量
                    if (!empty($menuList)) {
                        foreach ($menuList as $one => $two) {
                            if ($two['id'] == 3) {
                                $settle_num = $this->mSettle
                                    ->where(['settle_status' => 2, 'leader' => $dd_user_id, 'dept_id' => $userInfo['dept_id']])
                                    ->count();
                                $menuList[$one]['num'] = $settle_num > 99 ? 99 : $settle_num;
                            }
                        }
                    }
                } else if ($userInfo['level'] == 1) {
                    $menuList = $this->mMenu
                        ->field('id,p_id,name,level,icon,url, 0 as num')
                        ->where('','exp','FIND_IN_SET('.self::GLY.',auth)')
                        ->select();
                    // 待审核任务单2和待审核结算单3需要查询数量
                    if (!empty($menuList)) {
                        foreach ($menuList as $one => $two) {
                            if ($two['id'] == 2) {
                                $task_num = $this->mTask
                                    ->where(['task_status' => ['in', [1, 3, 5]], 'dept_id' => $userInfo['dept_id']])
                                    ->count();
                                $menuList[$one]['num'] = $task_num > 99 ? 99 : $task_num;
                            }
                            if ($two['id'] == 3) {
                                $settle_num = $this->mSettle
                                    ->where(['settle_status' => 3, 'dept_id' => $dept_id])
                                    ->count();
                                $menuList[$one]['num'] = $settle_num > 99 ? 99 : $settle_num;
                            }
                        }
                    }
                }

                break;
        }

        $menuList = collection($menuList)->toArray();

        return $this->get_attr($menuList,0);
    }


    /**
     * @introduction:根据传入数组生成新的递归数组
     * @param $arr
     * @param $pid
     * @return array
     * @author:cxh
     * @date: 2020/5/19 18:49
     */
    protected function get_attr($arr,$pid){
        $tree = array();
        foreach($arr as $v){
            if($v['p_id'] == $pid){
                $v['children'] = $this->get_attr($arr,$v['id']);
                if($v['children'] == null){
                    unset($v['children']);
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }


}




























